﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExpenseSystem.Model;
using ExpenseSystem.Fether;
using ExpenseSystem.DataUpdater;
using ExpenseSystem.Common;
using ExpenseSystem.Core.Authority;
using ExpenseSystem.DataUpdater.Validation;

namespace ExpenseSystem.Core.BonusModules
{
    public class SalesBonusUpdaterRegister
    {
        public NormalDataUpdater<SalesBonusModel> GetSalesBonusUpdater()
        {
            NormalDataUpdater<SalesBonusModel> systemDataUpdater = new NormalDataUpdater<SalesBonusModel>();
            systemDataUpdater.DataUpdating += DataNotSpaceValid;
            systemDataUpdater.DataUpdating += SingletonValid;
            systemDataUpdater.DataUpdating += StateValid;
            systemDataUpdater.DataUpdating += DocValid;
            systemDataUpdater.DataDeleting += CheckAvaliable;
            return systemDataUpdater;
        }

        private void CheckAvaliable(object sender, DataValidationEventArgs<SalesBonusModel> e)
        {
            if (e.Delete.State != Definition.STATE_NORMAL)
            {
                e.Message = String.Format("不允许删除已经审核或提审的单据{0}", e.Delete.Code);
                e.Cancle = true;
                return;
            }
        }

        private void SingletonValid(object sender, DataValidationEventArgs<SalesBonusModel> e)
        {
            string message;
            SalesBonusFetcher salesBonusFetcher = new SalesBonusFetcher();
            SalesBonusModel salesBonusModel = salesBonusFetcher.GetByCode(e.Insert.Site, e.Insert.Code, out message);
            if (salesBonusModel != null && salesBonusModel.AutoId != e.Insert.AutoId)
            {
                e.Message = String.Format("单号{0}已经被使用", e.Insert.Code);
                e.Cancle = true;
            }
        }

        private void DataNotSpaceValid(object sender, DataValidationEventArgs<SalesBonusModel> e)
        {
            if (String.IsNullOrWhiteSpace(e.Insert.Code))
            {
                e.Message = "单号不允许为空白";
                e.Cancle = true;
                return;
            }
            if (e.Insert.Bonus == 0)
            {
                e.Message = "不允许输入0元";
                e.Cancle = true;
            }
            string message;
            FiscalPeriodFetcher fiscalPeriodFetcher = new FiscalPeriodFetcher();
            FiscalPeriodModel fiscalPeriodModel = fiscalPeriodFetcher.GetDefault(e.Insert.Site, out message);
            if (fiscalPeriodModel == null)
            {
                e.Cancle = true;
                e.Message = String.Format("无法取得财务周期，单据无法保存:{0}", message);
                return;
            }
            DateTime dt1, dt2, dt3;
            dt1 = dt2 = dt3 = DateTime.MinValue;
            bool b1 = DateTime.TryParse(e.Insert.Date, out dt1);
            bool b2 = DateTime.TryParse(fiscalPeriodModel.OpenDate, out dt2);
            bool b3 = DateTime.TryParse(fiscalPeriodModel.DueDate, out dt3);
            if (!(b1 && b2 && b3))
            {
                e.Cancle = true;
                e.Message = "日期格式错误";
                return;
            }
            if (dt1 < dt2 || dt1 > dt3)
            {
                e.Cancle = true;
                e.Message = String.Format("单据日期{0}不在财务区间{1}-{2}内", dt1, dt2, dt3);
                return;
            }
        }
        private void StateValid(object sender, DataValidationEventArgs<SalesBonusModel> e)
        {
            if (e.Insert.State != Definition.STATE_NORMAL
                && (e.Insert.PropertyUpdated.Contains("CustomerId") ||
                e.Insert.PropertyUpdated.Contains("Code") ||
                e.Insert.PropertyUpdated.Contains("SalesId") ||
                e.Insert.PropertyUpdated.Contains("OrderId") ||
                e.Insert.PropertyUpdated.Contains("Date") ||
                e.Insert.PropertyUpdated.Contains("Bonus") 
                ))
            {
                e.Cancle = true;
                e.Message = "已提审/审核的单据不允许进行修改";
                return;
            }
        }
        private void DocValid(object sender, DataValidationEventArgs<SalesBonusModel> e)
        {
            string message;
            CustomerFetcher customerFetcher = new CustomerFetcher();
            if (customerFetcher.Get(e.Insert.Site, e.Insert.CustomerId, out message) == null)
            {
                e.Message = String.Format("单据{0}使用了不存在的客户{1}", e.Insert.Code, e.Insert.CustomerId);
                e.Cancle = true;
            }
            EmployeeFetcher employeeFetcher = new EmployeeFetcher();
            if (employeeFetcher.Get(e.Insert.Site, e.Insert.SalesId, out message) == null)
            {
                e.Message = String.Format("单据{0}使用了不存在的职员{1}", e.Insert.Code, e.Insert.SalesId);
                e.Cancle = true;
                return;
            }
            if (e.Insert.OrderId != 0)
            {
                OrderFetcher orderFetcher = new OrderFetcher();
                OrderModel orderModel = orderFetcher.Get(e.Insert.Site, e.Insert.OrderId, out message);
                if ( orderModel== null || orderModel.State == Definition.STATE_READY || orderModel.State == Definition.STATE_NORMAL)
                {
                    e.Cancle = true;
                    e.Message = String.Format("单据{0}使用了不存在的订单明细{1}", e.Insert.Code, e.Insert.OrderId);
                    return;
                }
            }
        }
    }
}
